<?php

namespace app\api\controller;


use app\common\model\DictItem;
use app\common\model\LoginUsers;
use app\common\model\MerchantCashier;
use think\Db;
use think\Request;
use think\Session;

class Personnel extends Pub
{

    //收银员列表
    public function cashier_list($param)
    {
        $map = [];
        if (!empty($param['name'])) {
            $map['a.xingming|a.tel'] = array('like', '%' . trim($param['name']) . '%');
        }
        $map['a.mid'] = ['in', $param['merchantid']];
        $page = ($param['page'] - 1) * 10;
        $LoginUsers = new LoginUsers();
        $list = $LoginUsers->alias('a')->join('wn_dict_item b','a.level=b.value','left')
            ->field('a.*,b.text as dict_name')
            ->where($map)->limit($page, 10)->select();
        if ($list){
            foreach ($list as $k=>$v){
                unset($v['password']);
            }
            $res_data['return_code'] =  "SUCCESS";
            $res_data['return_msg'] = "SUCCESS";
            $res_data['result_code'] = "SUCCESS";
            $res_data['result_msg'] = "成功";
            $res_data['info'] = $list;
        }else{
            $res_data['return_code'] =  "SUCCESS";
            $res_data['return_msg'] = "SUCCESS";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = "没有找到该员工";
        }
        return $res_data;
    }

    //用户添加
    public function cashier_add($param)
    {
        if (!empty($param['merchantid'])){
            $level = $param['level'];
            $data['xingming'] = $param['xingming'];
            $data['tel'] = $param['tel'];
            $data['gonghao'] = $param['gonghao'];
            $data['username'] = $param['username'];
            $data['password'] = MD5($param['password']);
            $data['level'] = $level;
            $mid = $param['merchantid'];
            $data['mid'] = $mid;
            $data['MerchantCashier'] = [
                'mid' => $param['merchantid'],
                'gonghao' => $param['gonghao'],
                'xingming' => $param['xingming'],
                'level' => $param['level'],
                'tel' => $param['tel'],
                'sex' => $param['sex'],
                'is_tz' => 1,
                'create_time' => time(),
            ];
            $LoginUser = new LoginUsers();
            $sav = $LoginUser->together('MerchantCashier')->validate("LoginUsers.add")->save($data);
            if ($sav) {
                switch ($level) {
                    case "3001":
                        $roleid = 4;
                        break;
                    case "3002":
                        $roleid = 5;
                        break;
                    case "3003":
                        $roleid = 6;
                        break;
                    case "3004":
                        $roleid = 9;
                        break;
                }
                $sdata['role_id'] = $roleid;
                $sdata['user_id'] = $LoginUser->id;
                $AuthRoleUser = new \app\common\model\AuthRoleUser();
                $AuthRoleUser->save($sdata);
                $res_data['return_code'] =  "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "SUCCESS";
                $res_data['result_msg'] = "成功";
            } else {
                $res_data['return_code'] =  "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "FAIL";
                $res_data['result_msg'] = $LoginUser->getError();
            }
        }else{
            $res_data['return_code'] =  "SUCCESS";
            $res_data['return_msg'] = "SUCCESS";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = "请选择门店后再添加人员";
        }
        return $res_data;
    }


    //收银员修改
    public function cashier_edit($param)
    {

        $level = $param['level'];
        $request['id'] = $param['id'];
        $data['xingming'] = $param['xingming'];
        $data['tel'] = $param['tel'];
        $data['gonghao'] = $param['gonghao'];
        if (!empty($param['password'])) {
            $data['password'] = MD5($param['password']);
        }
        $data['level'] = $level;
        $mid = $param['merchantid'];
        $data['mid'] = $mid;
        $data['status'] = $param['status'];
        $LoginUser = new LoginUsers();
        $sav = $LoginUser->allowField(true)->save($data, ['id' => $request['id']]);
        if ($sav) {
            switch ($level) {
                case "3001":
                    $roleid = 4;
                    break;
                case "3002":
                    $roleid = 5;
                    break;
                case "3003":
                    $roleid = 6;
                    break;
                case "3004":
                    $roleid = 9;
                    break;
            }
            $datas['gonghao'] = $param['gonghao'];
            $datas['xingming'] = $param['xingming'];
            $datas['level'] = $param['level'];
            $datas['tel'] = $param['tel'];
            $datas['sex'] = $param['sex'];
            $MerchantCashier = new MerchantCashier();
            $MerchantCashier->allowField(true)->save($datas, ['uid' => $param['id']]);
            $sdata['role_id'] = $roleid;
            $AuthRoleUser = new \app\common\model\AuthRoleUser();
            $AuthRoleUser->save($sdata, ['user_id' => $param['id']]);
            $res_data['return_code'] =  "SUCCESS";
            $res_data['return_msg'] = "SUCCESS";
            $res_data['result_code'] = "SUCCESS";
            $res_data['result_msg'] = "成功";
        } else {
            $res_data['return_code'] =  "SUCCESS";
            $res_data['return_msg'] = "SUCCESS";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $LoginUser->getError();
        }
        return $res_data;

    }

    //员工删除
    public function cashier_del($param)
    {
        $MerchantCashier = new MerchantCashier();
        $AuthRoleUser = new \app\common\model\AuthRoleUser();
        $AuthRoleUser->where(['user_id' => $param['id']])->delete();    //删除权限信息
        $LoginUser = new LoginUsers();
        $map['id'] = $param['id'];
        $res = $LoginUser->where($map)->delete();       //删除登陆账号
        $sql['uid'] = $param['id'];
        $MerchantCashier->where($sql)->delete();        //删除员工信息
        if ($res) {
            $res_data['return_code'] =  "SUCCESS";
            $res_data['return_msg'] = "SUCCESS";
            $res_data['result_code'] = "SUCCESS";
            $res_data['result_msg'] = "成功";
        } else {
            $res_data['return_code'] =  "SUCCESS";
            $res_data['return_msg'] = "SUCCESS";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $LoginUser->getError();
        }
        return $res_data;
    }




    //获取会员信息
    public function cashier_info($param)
    {
        try {
            $map['a.id'] = $param['id'];
            $LoginUser = new LoginUsers();
            $info = $LoginUser->alias('a')
                ->join('wn_merchant_cashier b','a.id=b.uid','left')
                ->field('a.xingming,a.tel,a.username,a.level,a.gonghao,a.id,a.status,b.mid,b.sex')
                ->where($map)->find();
            if ($info){
                $infos['xingming'] = $info['xingming'];
                $infos['tel'] = $info['tel'];
                $infos['username'] = $info['username'];
                $infos['level'] = $info['level'];
                $infos['gonghao'] = $info['gonghao'];
                $infos['id'] = $info['id'];
                $infos['status'] = $info['status'];
                $infos['mid'] = $info['mid'];
                $infos['sex'] = $info['sex'];
                //
                $res_data['return_code'] =  "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "SUCCESS";
                $res_data['result_msg'] = "成功";
                $res_data['info'] = $infos;
            }else{
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = '';
                $res_data['result_code'] = 'FAIL';
                $res_data['result_msg'] = '员工不存在';
            }

        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = '系统错误';
            $res_data['result_code'] = 'FAIL';
            $res_data['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $res_data;
    }

    //获取会员信息
    public function cashier_status($param)
    {
        try {
            $map['a.id'] = $param['id'];
            $LoginUser = new LoginUsers();
            $info = $LoginUser->alias('a')
                ->join('wn_merchant_cashier b','a.id=b.uid','left')
                ->field('a.xingming,a.tel,a.username,a.tel,a.level,a.gonghao,a.id,a.status,b.mid,b.sex')
                ->where($map)->find();
            if ($info){
                $data['status'] = $param['status'];
                $res = $LoginUser->where(['id'=>$info['id']])->update($data);
                if ($res) {
                    $res_data['return_code'] = "SUCCESS";
                    $res_data['return_msg'] = "SUCCESS";
                    $res_data['result_code'] = "SUCCESS";
                    $res_data['result_msg'] = "成功";
                }else{
                    $res_data['return_code'] = "SUCCESS";
                    $res_data['return_msg'] = "SUCCESS";
                    $res_data['result_code'] = "FAIL";
                    $res_data['result_msg'] = "失败";
                }
            }else{
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = '';
                $res_data['result_code'] = 'FAIL';
                $res_data['result_msg'] = '员工不存在';
            }

        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = '系统错误';
            $res_data['result_code'] = 'FAIL';
            $res_data['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $res_data;
    }

    //获取员工角色信息
    public function cashier_role($param){
        try {
            $LoginUser = new LoginUsers();
            $DictItem = new DictItem();
            $userinfo = $LoginUser->where(['username'=>$param['username']])->find();
            if ($userinfo) {
                if ($userinfo['level'] == '3002') {
                    $dice = $DictItem->where(['value' => 3003])->select();
                } elseif ($userinfo['level'] == '3001') {
                    $map['value'] = ['in', '3002,3003'];
                    $dice = $DictItem->where($map)->select();
                } else {
                    $dice = [];
                    $dice = $DictItem->where(['value' => 3003])->select();
                }
//                $res_data['$userinfo'] = $userinfo;
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "SUCCESS";
                $res_data['result_msg'] = "成功";
                $res_data['info'] = $dice;
            }else{
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "FAIL";
                $res_data['result_msg'] = "用户信息异常";
            }
        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = '系统错误';
            $res_data['result_code'] = 'FAIL';
            $res_data['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $res_data;
    }

}
